package com.shadow.baiqun.delegate.receive.locationcheck;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.shadow.baiqun.config.ItemField;
import com.shadow.baiqun.config.ItemType;
import com.shadow.baiqun.global.LocationEntity;
import com.shadow.ui.recycler.DataConverter;
import com.shadow.ui.recycler.MultipleItemEntity;
import com.shadow.ui.recycler.ObjConverter;

import java.util.ArrayList;

/**
 * @author zy
 * on 2018/7/7
 */
public class LocationCheckConverter extends DataConverter {
    @Override
    public ArrayList<MultipleItemEntity> convert() {
        try {
            final JSONArray jsonArray = JSONObject.parseObject(getJsonData())
                    .getJSONArray("rows");
            final int size = jsonArray.size();
            for (int i = 0; i < size; i++) {
                final JSONObject object = jsonArray.getJSONObject(i);
                LocationCheckEntry locationCheckEntry = new ObjConverter<LocationCheckEntry>().
                        convert(object.toJSONString(), new TypeReference<LocationCheckEntry>() {
                        });
                MultipleItemEntity itemEntity = MultipleItemEntity.builder()
                        .setItemType(locationCheckEntry.getStockStatus() == 0 ?
                                ItemType.ITEM_LOCATION_UNCHECK : ItemType.ITEM_LOCATION_CHECKED)
                        .setField(ItemField.ITEM_OBJ, locationCheckEntry)
                        .build();
                mEntities.add(itemEntity);
            }
            return mEntities;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public ArrayList<MultipleItemEntity> convertLocation() {
        try {
            final JSONArray jsonArray = JSONObject.parseObject(getJsonData())
                    .getJSONArray("rows");
            final int size = jsonArray.size();
            for (int i = 0; i < size; i++) {
                final JSONObject object = jsonArray.getJSONObject(i);
                LocationEntity locationEntity = new ObjConverter<LocationEntity>().
                        convert(object.toJSONString(), new TypeReference<LocationEntity>() {
                        });
                MultipleItemEntity itemEntity = MultipleItemEntity.builder()
                        .setItemType(ItemType.ITEM_LOCATION)
                        .setField(ItemField.ITEM_OBJ, locationEntity)
                        .build();
                mEntities.add(itemEntity);
            }
            return mEntities;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
